﻿<%@page import="com.pb.news.util.PageSupport"%>
<%@page import="com.pb.news.entity.News"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../common/common.jsp" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<script type="text/javascript">
//<!--
	function addNews(){
		window.location="newsDetailCreate.jsp";
	}
	
//-->
</script>
  <div class="main-content-right">
        <!--即时新闻-->
        <div class="main-text-box">
            <div class="main-text-box-tbg">
                <div class="main-text-box-bbg">
                    <form name ="searchForm" id="searchForm" action="/news5/jsp/admin/newsDetailList.jsp" method="post">
		 	<div>
		 				新闻分类：
		 					<select name="categoryId">
		 						<option value="0">全部</option>
			        			
			        				<option value='1' >国内</option>
			        			
			        				<option value='2' >国际</option>
			        			
			        				<option value='3' >娱乐</option>
			        			
			        				<option value='4' >军事</option>
			        			
			        				<option value='5' >财经</option>
			        			
			        				<option value='6' >天气</option>
			        			
	        				</select>
		 				新闻标题<input type="text" name="title" id="title" value=''/>
		 					<button type="submit" class="page-btn">GO</button>
		 					<button type="button" onclick="addNews();" class="page-btn">增加</button>
		 		<input type="hidden" id="pageIndex" name="pageIndex" value="1"/>
		 	</div>
		 	</form>
			<table cellpadding="1" cellspacing="1" class="admin-list">
				<thead >
					<tr class="admin-list-head">
						<th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <%
                // 页面容量
                int pageSize = 2;
                // 当前页面页码
                int currentPageNo = 1;
                String pageIndex = request.getParameter("pageIndex");
                if(pageIndex == null){
                	currentPageNo = 1;
                }else{
                	try{
                		currentPageNo = Integer.parseInt(pageIndex);
                	}catch(NumberFormatException e){
                		response.sendRedirect("error.jsp");
                	}
                }
                // 总记录数
                int totalCount = newsService.getNewsCount();
                System.out.println(totalCount);
                // 总页面数
              	PageSupport pageSupport = new PageSupport();
                pageSupport.setPageSize(pageSize);
                pageSupport.setCurrentPageNo(currentPageNo);
                pageSupport.setTotalCount(totalCount);
                int totalPageCount = pageSupport.getTotalPageCount();
                
                //首页和尾页的异常处理
                if(currentPageNo < 1){
                	currentPageNo = 1;
                }
                if(currentPageNo > totalPageCount){
                	currentPageNo = totalPageCount;
                }
              
               	List<News> newsList=newsService.getPagesNewsList(currentPageNo, pageSize);
               	request.setAttribute("newsList", newsList);
               	/* int i=0;
               	for(News news:newsList){
               		request.setAttribute("news", news);
               		i++; */
                %>
                <tbody>
                <c:forEach var="news" items="${newsList}" varStatus="status">
                	<tr <c:if test="${status.count%2==0 }"> class="admin-list-td-h2"</c:if>>
                		<%-- <td><a href='adminNewsView.jsp?id=<%=news.getId()%>'><%=news.getTitle() %></a></td> --%>
                		<td><a href='adminNewsView.jsp?id=${news.id}'>
                		<c:out value="${news.title}"></c:out></a></td>
                		<%-- <td><%=news.getAuthor()%></td> --%>
                		<!-- 注意null和空字符串的区别，下面的default替换的是null -->
                		<td><c:out value="${news.author}" default="暂无"></c:out></td>
                		<%--<td><%=news.getCreateDate() %></td>--%>
                		<td><fmt:formatDate value="${news.createDate}" pattern="yyyy-MM-dd"/></td>
                		<!-- <td><a href='adminNewsCreate.jsp?id=2'>修改</a> -->
                		<td><a href='<c:url value="adminNewsEdit.jsp" >
                			<c:param name="id" value="${news.id}"></c:param>
                		</c:url>'>修改</a>
                			<a href="javascript:if(confirm('确认是否删除此新闻？')) location='adminNewsDel.jsp?id=2'">删除</a>
                		</td>
                	</tr> 
                </c:forEach>
                </tbody>
                <%
                	//}
                %>
            </table>
            <input type="hidden" id="totalPageCount" value="<%=totalPageCount%>"/>
           <c:import url="rollPage.jsp">
           	<c:param name="totalCount" value="<%=Integer.toString(totalCount) %>"></c:param>
        	<c:param name="totalPageCount" value="<%=Integer.toString(totalPageCount) %>"></c:param>
        	<c:param name="currentPageNo" value="<%=Integer.toString(currentPageNo) %>"></c:param>
           </c:import>
        </div>
       </div>
   </div>
   </div>
</div>